package com.dave.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Dave
 * @Date 2018/8/10
 * @Description Stream收集器
 */
public class Collector {
    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT), new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));


        //类似count
        Long collect = menu.stream().collect(Collectors.counting());

        //最高热量
        menu.stream().max(Comparator.comparingInt(Dish::getCalories));

        //汇总累加 类似于sum()
        menu.stream().collect(Collectors.summingInt(Dish::getCalories));

        //平均数
        menu.stream().collect(Collectors.averagingInt(Dish::getCalories));

        //将结果收集 并提供各种信息
        IntSummaryStatistics intSummaryStatistics = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
        intSummaryStatistics.getAverage();
        intSummaryStatistics.getMax();
        intSummaryStatistics.getMin();

        //拼接字符串
        menu.stream().map(Dish::getName).collect(Collectors.joining(":"));

        //菜单的总热量
        menu.stream().collect(Collectors.reducing(0, Dish::getCalories, Integer::sum));




    }
}
